package com.hmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdmall.business.common.common.ResponseCode;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallAreaMapper;
import com.hmall.mapper.MallShippingMapper;
import com.hmall.model.MallArea;
import com.hmall.model.MallShipping;
import com.hmall.model.ResultInfo;
import com.hmall.vo.HxmallReslultInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ShippingService {

    @Autowired
    private MallShippingMapper shippingMapper;

    @Autowired
    private MallAreaMapper areaMapper;

    public ServerResponse add(Integer userId, MallShipping shipping) {

        if (shipping == null) ServerResponse.createByErrorMessage("创建地址失败");
        if(shipping.getDefaultCheck() == null) shipping.setDefaultCheck(0);
        shipping.setUserId(userId);

        int resultCount = shippingMapper.myInsert(shipping);
        System.out.println("插入完成");
        return ServerResponse.createBySuccess("新建地址成功");
    }

    /**
     * louxinlong
     * 添加发货地址
     *
     * @return
     */
    public ResultInfo insertShipping(Integer userId, MallShipping shipping) {

        if (null == shipping) {
            return HxmallReslultInfoVo.build("", "发货地址不能空", "");
        }
        shipping.setUserId(userId);
        shippingMapper.insert(shipping);
        return HxmallReslultInfoVo.build("200", "发货地址添加成功", shipping);
    }


    public ServerResponse findprovince(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List province = areaMapper.selectProvince();
        PageInfo pageInfo = new PageInfo(province);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse finddetail(int pageNum, int pageSize, Integer id) {

        PageHelper.startPage(pageNum, pageSize);
        List detail = areaMapper.selectDetail(id);
        PageInfo pageInfo = new PageInfo(detail);
        return ServerResponse.createBySuccess(pageInfo);
    }


    public ServerResponse<String> del(Integer userId, Integer shippingId) {

        //不能删除
        //更改为不可用
        try {
            shippingMapper.deleteByShippingIdUserId(userId, shippingId);
            return ServerResponse.createBySuccess("删除地址成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("删除地址失败");
        }

    }


    public ServerResponse update(Integer userId, MallShipping shipping) {

        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateByShipping(shipping);
        if (rowCount > 0) {
            return ServerResponse.createBySuccess("更新地址成功");
        }
        return ServerResponse.createByStatus(ResponseCode.SHIPPING_UPDATE_ERROR.getCode(), ResponseCode.SHIPPING_UPDATE_ERROR.getDesc(), null);
    }
    /* *//**
     * 把已默认的取消
     * @param userId
     * @return
     *//*
    public ServerResponse updateNoDefaultShipping(Integer userId){
        int rowCount = shippingMapper.updateNoDefaultShipping(userId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess(取消默认成功");
        }
        return ServerResponse.createByStatus(ResponseCode.SHIPPING_UPDATE_ERROR.getCode(),ResponseCode.SHIPPING_UPDATE_ERROR.getDesc(),null);

        return HxmallReslultInfoVo.build("405","取消默认失败","");
    }
    */

   /*
    public ResultInfo updateByUseridDefault(Integer userId, MallShipping shipping){
        if(null==shipping.getId()){
            return HxmallReslultInfoVo.build("406","地址编号找不到","");
        }
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateDefaultShipping(shipping);
        if(rowCount > 0){
            return HxmallReslultInfoVo.build("200","更新地址成功","");
        }
        return HxmallReslultInfoVo.build("405","更新地址失败","");
    }*/
    public ServerResponse<MallShipping> select(Integer userId, Integer shippingId) {
        MallShipping shipping = shippingMapper.selectByShippingIdUserId(userId, shippingId);
        if (shipping == null) {
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("更新地址成功", shipping);
    }


    public ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);

        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    //根据用户查询发货地址
    public ServerResponse selectShippingList(Integer userId) {
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);
        if (null == shippingList || shippingList.size() == 0) {
            return ServerResponse.createByStatus(ResponseCode.SHIPPING_NOT_FOUND.getCode(), ResponseCode.SHIPPING_NOT_FOUND.getDesc(), null);
        }
        return ServerResponse.createBySuccess("发货地址查询成功");
    }

    //查询默认发货地址
    public ServerResponse<MallShipping> selectDefaultShipping(Integer userId) {
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);
        if (null == shippingList || shippingList.size() == 0) {
            return ServerResponse.createByStatus(ResponseCode.SHIPPING_NOT_FOUND.getCode(), ResponseCode.SHIPPING_NOT_FOUND.getDesc(), null);
        }
        Boolean f = false;
        MallShipping shippingDef = new MallShipping();
        for (MallShipping shipping : shippingList) {
            if (shipping.getDefaultCheck() == 1) {
                shippingDef = shipping;
                f = true;
                break;
            }
        }
        if (!f) {
            shippingDef = shippingList.get(0);
        }
        return ServerResponse.createBySuccess(shippingDef);
    }

    public ServerResponse<List<MallShipping>> list(Integer userId) {

        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);
        return ServerResponse.createBySuccess(shippingList);
    }

    public ServerResponse<MallShipping> selectByShippingId(int shippingId) {

        MallShipping mallShipping = shippingMapper.selectByPrimaryKey(shippingId);
        return ServerResponse.createBySuccess(mallShipping);
    }

    public MallShipping selectByShippingId(Integer shippingId) {

        return shippingMapper.selectByPrimaryKey(shippingId);
    }

    public ServerResponse<MallShipping> findShip(Integer shippingId) {

        MallShipping mallShipping = shippingMapper.selectByPrimaryKey(shippingId);
        return ServerResponse.createBySuccess(mallShipping);
    }
}
